//
//  MyCenterModel.m
//  Yihubang
//
//  Created by hyu on 16/7/25.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "MyCenterModel.h"
#import "MyCenterCell.h"
//#import "MyAdressContractsVC.h"

@implementation MyCenterModel
- (void)initialize {
    [super initialize];
//    [self.navigationBar setHidden:NO];
    self.title = @"我的";
//    [self loadLocalData];
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(RefurbishMyInfo) name:@"RefurbishMyInfo" object:nil];
    //    [self initMenu];
    @weakify(self)
    [[[RACObserve(self, dataSource)
       filter:^(NSArray *events) {
           return @(events.count > 0).boolValue;
       }]
      deliverOnMainThread]
     subscribeNext:^(NSArray *events) {
         @strongify(self)
         //         [self.mTableView beginUpdates];
         [self.mTableView reloadData];
         //         [self.mTableView endUpdates];
     }];
}
-(void)RefurbishMyInfo{
    //添加用户行为记录
    [self loadLocalData];
    [self.viewController addUserBehaviourString:@"UserCenter_View"];
    @weakify(self)
    [[self.httpService queryUserCenterWith:nil] subscribeNext:^(BaseModel *model){
        @strongify(self)
        if (model.code==200) {
            [[UserClient sharedUserClient] setUserInfo:model.data];
            self.userInfo = model.data;
        }else {
            self.userInfo = self.viewController.userClient.userInfo;
            [self.viewController hiddenHUDWithString:@"获取信息失败" error:NO];
        }
    }];
  }
- (void)loadLocalData{
    NSMutableArray *results=[NSMutableArray array];
    results=[self initmenu];
    NSMutableArray *data = [NSMutableArray new];
    NSMutableArray *tempGroup =[NSMutableArray new];
    for (MineMenuModel *model in results) {
        [tempGroup addObject:model];
    }
    [data addObject:tempGroup];
    self.dataSource = data;
}

-(void)bindTableView:(UITableView *)tableView{    
    [super bindTableView:tableView];
    self.cellReuseIdentifier = @"MyCenterCell";
    [tableView registerCellWithReuseIdentifier:self.cellReuseIdentifier];
}
-(void)configureCell:(MyCenterCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(MineMenuModel*)object{
    [cell bindViewModel:object];
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * titleView = [UIView new];
    titleView.frame = CGRectMake(0, 0, ScreenWidth - 148.f, 70);
    UILabel * title = [UILabel new];
    title.text = @"信息";
    title.font =[UIFont systemFontOfSize:15.f];
    title.textColor = [UIColor whiteColor];
    [titleView addSubview:title];
    UIImageView * image = [UIImageView new];
    image.image = [UIImage imageNamed:@"信息"];
    [titleView addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleView).with.offset(30);
        make.top.equalTo(titleView).with.offset(30);
    }];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(image).with.offset(25);
        make.top.equalTo(titleView).with.offset(30);
    }];
    UILabel * line = [UILabel new];
    [line setBackgroundColor:[UIColor whiteColor]];
    [line setAlpha:0.8f];
    [titleView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleView).with.offset(30);
        make.right.equalTo(titleView).with.offset(-47);
        make.bottom.equalTo(titleView).with.offset(0);
        make.height.equalTo(@0.5);
    }];
    
    return titleView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 70.f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *identifierStr = @"";
    switch (indexPath.row) {
        case 0:
            identifierStr = @"MyContactVC";
            break;
        case 1:
            identifierStr = @"SetSecurityCodeViewController";
            break;
        case 2:
            identifierStr = @"SetSettingViewController";
           
            break;
        case 3:
            identifierStr = @"IMViewController";
//            identifierStr = @"MyAdressContractsVC";
            break;
            
        case 4:{
            NSUserDefaults* userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setObject:nil forKey:DeterrenceFirstKey];
            [userDefault setObject:nil forKey:CallHelpFirstKey];
            [userDefault setObject:nil forKey:MyCenterFirstKey];
            [userDefault synchronize];
            UIViewController *mainVct = [UIStoryboard viewController:@"GuardMe" identifier:@"CenterAndDeterenceRootNAV"];
            [self.viewController presentViewController:mainVct animated:NO completion:^(void){
            }];

        }
            break;
//        case 4:
//            identifierStr = @"SetSecurityCodeViewController";
//            break;
//        case 5:
//            identifierStr = @"SetSettingViewController";
//            break;
            
        default:
            break;
    }
    if ([identifierStr isEqualToString:@""]) {
        NSLog(@"无点击事件");
    }else{
    [self pushto:identifierStr with:0];
    }
}


-(void)pushto:(NSString *)identifier with:(NSInteger) index{
//    if ([identifier isEqualToString:@"MyAdressContractsVC"]) {
//        UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//        BaseViewController * BaseViewController = [HelpStory instantiateViewControllerWithIdentifier:identifier];
//        [self .viewController pushViewController:BaseViewController animated:YES];
//    }else{
    UIStoryboard *HelpStory = [UIStoryboard storyboardWithName:@"Mine" bundle:[NSBundle mainBundle]];
    BaseViewController * BaseViewController = [HelpStory instantiateViewControllerWithIdentifier:identifier];

    [self .viewController pushViewController:BaseViewController animated:YES];
//    }
}
-(NSMutableArray *) initmenu{
    NSMutableArray *results=[NSMutableArray array];
    MineMenuModel *model;
    model = [MineMenuModel new];
    model.title = @"紧急联系人添加";
    model.icon = @"";
    model.orderNum = 1;
    [results addObject:model];
    model = [MineMenuModel new];
    model.title = @"遗忘/修改安全码";
    model.icon = @"";
    model.orderNum = 2;
    [results addObject:model];
    model = [MineMenuModel new];
    model.title = @"设置";
    model.icon = @"";
    model.orderNum = 3;
    [results addObject:model];
    model = [MineMenuModel new];
    model.title = @"我的好友";
    model.icon = @"";
    model.orderNum = 4;
    [results addObject:model];
    return results;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
